﻿/** \file
* \$Rev: 186 $
* 
* \$Date: 2010-04-16 05:58:01 +0000 (Fri, 16 Apr 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/Program.cs $
*/

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using chorus.twitter;
using chorus.utils;

/// <summary>
/// The base namespace for the chorus application: contains main program stuff.
/// </summary>
namespace chorus
{
	/// <summary>
	/// The chorus gui namespace: contains form and control classes.
	/// </summary>
	namespace gui { }

	/// <summary>
	/// The chorus net namespace: contains code for network and XML
	/// transactions.
	/// </summary>
	namespace net { }

	/// <summary>
	/// The chorus twitter namespace: contains Twitter-related XML
	/// deserialization classes.
	/// </summary>
	namespace twitter { }

	/// <summary>
	/// The namespace containing various utility classes.
	/// </summary>
	namespace utils { }

	/// <summary>
	/// The main program class.
	/// </summary>
	public static class Program
	{
		/// <summary>The main application form.</summary>
		public static readonly gui.MainForm mainform;

		/// <summary>The list of all views.</summary>
		public static readonly DisposableBindingList<net.View> views =
			new DisposableBindingList<net.View>();
		/// <summary>The list of all users.</summary>
		public static readonly UserCollection<user> users =
			new UserCollection<user>();
		/// <summary>The list of all accounts.</summary>
		public static readonly UserCollection<Account> accounts =
			new UserCollection<Account>();
		/// <summary>The list of all statuses.</summary>
		public static readonly KeyedCollection<status> statuses =
			new KeyedCollection<status>();
		/// <summary>The list of all direct messages.</summary>
		public static readonly KeyedCollection<direct_message> dms =
			new KeyedCollection<direct_message>();

		/// <summary>Whether to raise a dialogue box when an exception occurs.</summary>
		public static bool GuiErrors { get; set; }


		/// <summary>The program's static constructor (what the hell?)</summary>
		static Program()
		{
			GuiErrors = true;

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			mainform = new gui.MainForm();
		}

		/// <summary>The main entry point for the application.</summary>
		[STAThread]
		static void Main() { Application.Run(mainform); }

		/// <summary>Handle an exception, optionally showing it to the user.</summary>
		/// <param name="e">The exception.</param>
		public static void HandleException(Exception e) { HandleException(e, ""); }

		/// <summary>Handle an exception, optionally showing it to the user.</summary>
		/// <param name="e">The exception.</param>
		/// <param name="added">An additional error message.</param>
		public static void HandleException(Exception e, string added)
		{
			if (!GuiErrors)
				throw e;
			string message = added + Environment.NewLine;
			while (e != null)
			{
				message += e.Message + Environment.NewLine;
				e = e.InnerException;
			}
			MessageBox.Show(message, "Error");
		}
	}
}
